package com.kobe.system.controller;

import com.kobe.common.core.page.TableDataInfo;
import com.kobe.system.domain.SysKobeDetail;
import com.kobe.system.domain.SysKobeFinance;
import com.kobe.system.service.SysKobeFinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.kobe.common.core.controller.BaseController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/kobe/finance")
public class SysKobeFinanceController extends BaseController{

    @Autowired
    private SysKobeFinanceService sysKobeFinanceService;


    @PostMapping("/update")
    public void update(@RequestBody SysKobeFinance sysKobeFinance) {
        System.out.println(sysKobeFinance.getDetailList());
        sysKobeFinanceService.updateFinance(sysKobeFinance);
        List<SysKobeDetail> detailList = sysKobeFinance.getDetailList();
        //遍历这个detailList数组,每个对象都执行一次添加
        detailList.forEach(detail -> {
            sysKobeFinanceService.addDetail(detail);
        });
    }

    @PostMapping("/add")
    public void add(@RequestBody SysKobeFinance sysKobeFinance) {
        sysKobeFinanceService.add(sysKobeFinance);
    }

    @GetMapping("/list")
    public TableDataInfo listFinance(SysKobeFinance sysKobeFinance) {
        startPage();
        List<SysKobeFinance> list = sysKobeFinanceService.listFinance(sysKobeFinance);
        return getDataTable( list);
    }

    @GetMapping("/getFinanceById/{financeId}")
    public SysKobeFinance get(@PathVariable Long financeId) {
        startPage();
        SysKobeFinance  finance  = sysKobeFinanceService.getFinanceById(financeId);
        List<SysKobeDetail> list1 = sysKobeFinanceService.getDetailById(financeId);
        finance.setDetailList(list1);
        return finance;
    }

    @GetMapping("/getFinanceByProjectId/{projectId}")
    public TableDataInfo getFinanceByProjectId(@PathVariable Long projectId) {
        startPage();
        List<SysKobeFinance> list = sysKobeFinanceService.getFinanceByProjectId(projectId);
        return getDataTable(list);
    }

    @GetMapping("/delete/{projectId}")
    public void delete(@PathVariable Long projectId) {
        sysKobeFinanceService.deleteFinanceById(projectId);
    }

    @GetMapping("/getDetailMoneyByFinanceId/{financeId}")
    public List<SysKobeDetail> getDetailMoneyByFinanceId(@PathVariable Long financeId) {
        return sysKobeFinanceService.getDetailById(financeId);
    }
}
